/**
 * @author 吴汶
 * @since 2022/10/14
 * 14:26
 */
package com.windgo.controller.wx;

import com.windgo.bean.MarketAddress;
import com.windgo.bean.MarketUser;
import com.windgo.bean.WxMarketAddress;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.BaseRespVo;
import com.windgo.bean.common.CommonData;
import com.windgo.service.AddressService;
import com.windgo.util.PatternUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("wx/address")
public class WxAddressController {
    /**前台收货地址管理
     * @author 吴汶
     * @since 2022/10/14
     * 14:26
     */
    @Autowired
    AddressService addressService;
    @RequestMapping("list")
    public BaseRespVo list(Integer id){
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser)principals.getPrimaryPrincipal();
        Integer id1 = primaryPrincipal.getId();
        CommonData<WxMarketAddress> data = addressService.wxList( id1);
        return BaseRespVo.ok(data);
    }
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketAddress marketAddress){
        Integer id = marketAddress.getId();
        Integer i = addressService.delete(id);
        if(i!=1){
            return null;
        }
        return BaseRespVo.ok();
    }
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id){
        WxMarketAddress data = addressService.selectById(id);
        return BaseRespVo.ok(data);
    }
    @PostMapping("save")
    public BaseRespVo save(@RequestBody MarketAddress marketAddress){
        //首先进行验证
        //使用正则表达式进行校验
        if(!PatternUtils.isMobile(marketAddress.getTel())){
            return BaseRespVo.error();
        }
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
        Integer id = addressService.update(marketAddress,userId);
       // Integer id = update.getId();
        return BaseRespVo.ok(id);
    }
}

